<?php

namespace app\api\controller;

use other\zongan\ZhongAn;
use think\facade\Log;

class Oauth
{
    /**
     * 众安保险跳转的路径
     */
    public function auth()
    {
        $code = input('code');
        if (empty($code)) {
            return fail('code为空');
        }

        Log::info('众安请求的code'.$code);

        //防止两次跳转
        $codeCacheKey = $code.'zhongan';

        if (cache($codeCacheKey)){
            return success('',[
                'phone' => cache($codeCacheKey)
            ]);
        }

        $data = (new ZhongAn())->getPhoneByCode($code);
        if (empty($data['value']['phone'])) {
            return fail('', 0, $data);
        }

        $phone = !empty($data['value']['phone']) ? $data['value']['phone'] : '';

        cache($codeCacheKey,$phone,3600);

        return success('',compact('phone'));

    }
}